﻿#ifndef TRANSDLG_H
#define TRANSDLG_H

#include <QDialog>
#include <QTreeWidgetItem>
#include "db_cfg_g_def.h"
#include "DbCfgFun.h"
#include "GetColumnInfo.h"

#define	 TRANS_PROP_NONE					0			//无任何操作
#define	 TRANS_PROP_UPDATE					1			//修改
#define	 TRANS_PROP_INSERT					2			//添加
#define	 TRANS_PROP_DELETE					3			//删除


namespace Ui {
    class TransDlg;
}

class TransDlg : public QDialog {
    Q_OBJECT
public:
    TransDlg(int TableId, int ParentId, QWidget *parent = 0);
    ~TransDlg();

protected:

	//初始化列表头
	void InitTableHeadItem();

	//初始化树显示
	void InitTreeShow();
	void InitTreeItem_Task(QTreeWidgetItem *pParentItem);
	void InitTreeItem_Dev(QTreeWidgetItem *pParentItem, unsigned int ParentId);


	//显示选中设备的点内容
 	void ShowIndexInfo();

	//添加一条到显示
	void AddOneIndexToShow(Trans_ItemData ItemData, QString IndexName);

	//删除点信息
	void DeleteOneIndexInfo(unsigned int DevId, unsigned int IndexId);

	//删除所有的点信息
	void DeleteAllIndexInfo();

	//显示转发信息
 	void ShowTransInfo();

	//添加到转发信息显示
	int AddOneToTransInfoShow(Trans_ItemData ItemData, int RowIndex);

	//设置转发信息删除标志
	int SetDeleteFlagTransInfo(int RowIndex, bool bDeleteFlag);

	//检查当前点是否已存在转发表中
	bool CheckIndexIsInTransInfo(unsigned int DevId, unsigned int IndexId);	

	//设置树Item
	int SetItem(QTreeWidgetItem *pTreeItem, QString ItemText, QVariant ItemData);

	//	获取选中点记录行
	int GetSelectIndexRow();

	//	获取选中点记录信息
	void GetSelectIndexInfo();

	//	获取选中转发记录
	int GetSelectTransRow();

	//调整转发序号
	void AdjustTransNo();

	//添加一条转发信息显示
	int InsertOneTransinfoShow(unsigned int DevId, unsigned int IoId);

	//从数据库中删除一条转发记录
	int DeleteOneFromTransDB(unsigned int RecordId);

	////从数据库中删除全部转发记录
	int DeleteAllFromTransDB();

	//修改转发记录转发序号
	int upDateTransNoDB(unsigned int RecordId, unsigned short TransNo);

	//插入转发记录
	int insertTransInfoDB(unsigned int DevId, unsigned int IndexId, unsigned short TransNo);
	
	void SaveData();

	void	SaveData_New();
	
	//从数据库中删除一条转发记录
	int		DeleteOneFromTransDB_New(unsigned int RecordId);
	//修改转发记录转发序号
	int		upDateTransNoDB_New(unsigned int RecordId, unsigned short TransNo);

	//插入转发记录
	int		insertTransInfoDB_New(unsigned int DevId, unsigned int IndexId, unsigned short TransNo);
	
	void	CommitDeleteOper();
	void	CommitUpDateOper();
	void	CommitInserterOper();
protected:
    void changeEvent(QEvent *e);
	
protected slots:
	void	OnClickTreeItem(QTreeWidgetItem *pCurItem);
	void	Affirm();
	void	SaveBtnClick();
	void	AddSeltIoInfo();
	void	AddAll();
	void	DeleteSeltTrans();
	void	DeleteAllTrans();
	void	CancelDeleteTrans();
	void	IndexTabDoubleClick();

private:
	Ui::TransDlg *ui;


	QTreeWidgetItem *m_LastSelectItem;
	int	m_CurTransTabId;
	int	m_CurIoTableId;
	int	m_ParentId;				//
	int	m_TransInfoId;			//转发信息ID
	unsigned int	m_CurSeltDevId;			//当前选中的设备
	unsigned int	m_CurSeltIoId;			//当前选中的点
	unsigned int	m_CurSeltTransId;		//当前选中的转发点
	bool	m_bDeltTransFlag;		//删除转发标志
	bool	m_bDeltAllFlag;			//全部删除标志
	GetColumnInfo	m_ColInfo;
	char	*m_RecordBuff;
	char	m_CurTransTableName[TABLE_ENG_NAME_LEN];
	QString			 m_deltSqlStr;
	QString			 m_updateSqlStr;
	QString			 m_insertSqlStr;
	char			 m_sql[SQL_MAX_LEN*2];
	unsigned int	 m_StartRecordId;
};

#endif // TRANSDLG_H
